<?php

namespace app\app\controller;


use app\common\controller\Homebase;
use EasyWeChat\Factory;
use think\Db;
use util\Date;
use util\File;
use function Symfony\Component\String\u;

class Dl extends WxUser
{
    protected $Dl_info;
    protected $role;
    protected $noRule = ['getGoodsDetail'];

    protected function initialize()
    {
        parent::initialize();
        $member_dl_data = Db::name('member_dl')->where([
            'member_id' => $this->memberId,
            'is_del' => 0
        ])->find();
        if (!$member_dl_data) {
            responseJson(-300, '代理信息不存在');
        }
        $this->Dl_info = $member_dl_data;
    }
    //代理顶部信息
    //账户中心四个
    public function accountCenter()
    {
        //可提现
        $a = $this->memberInfo['money'];
        $b = Db::name('reward')->where([
            'member_id' => $this->memberId,
            'status' => 0
        ])->sum('money');
        $c = Db::name('member_finance')->where([
            ['member_id', '=', $this->memberId],
            ['type', '=', 6],
            ['is_kuan', '=', 0],
        ])->sum('money');
        $d = Db::name('member_finance')->where([
            ['member_id', '=', $this->memberId],
            ['type', '=', 6],
            ['is_kuan', '=', 1],
        ])->sum('money');
        $return['a'] = $a;
        $return['b'] = $b;
        $return['c'] = $c;
        $return['d'] = $d;
        msg(0, '查询成功', $return);
    }

    //提现记录
    public function tixianList()
    {
        $data = $this->request->param();
        $page = input('page/d', 1);
        $length = input('limit/d', 20);
        $data_list = Db::name('member_finance')
            ->where([
                ['member_id', '=', $this->memberId],
                ['type', 'in', [6]],
            ])
            ->order('create_time desc')
            ->paginate($length, false, [
                'page' => $page
            ])->each(function ($item) {
                $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                //is_kuan 是否打款 0没有 1已打款
                return $item;
            });
        msg(0, '查询成功', $data_list);
    }
    //提现
    public function tixian()
    {
        $data = $this->request->param();
        if ($data['money'] <= 0) {
            msg(-1, '输入正确值', '');
        }
        if ($data['money'] > $this->memberInfo['money']) {
            msg(-1, '提现金额不能大于余额', '');
        }
//        if ($data['money'] < $this->memberInfo['min_tx']) {
//            msg(-2, '最低提现金额' . $this->memberInfo['min_tx'], '');
//        }
        Db::startTrans();
        try {
            $insert_data = array(
                'member_id' => $this->memberId,
                'money' => $data['money'],
                'type' => 6,
                'tixian_type' => $data['tixian_type'], //1银行 2支付宝 3微信
                'tixian_account' => $data['tixian_account'], //提现账号
                'tixian_name' => $data['tixian_name'],//提现人姓名
                'tixian_bank' => $data['tixian_bank'],//提现银行名（银行提现使用）
                'desc' => '余额提现',
                'status' => 0,//'0未完成 待审核 1已完成 审核完成 提现使用',
                'create_time' => time(),
                'is_count' => 2
            );
            $insert_data['shou_money'] = round($data['money'] * (0.07), 2);
            $insert_data['shou_money'] = 0;
            $insert_data['true_money'] = $data['money'] - $insert_data['shou_money'];
            $insert_id = Db::name('member_finance')->insertGetId($insert_data);
            Db::name('member')
                ->where('id', '=', $this->memberId)
                ->setDec('money', $data['money']);
            Db::commit();
            msg(0, '申请成功', $insert_id);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }
    //用户列表
    public function myTeam()
    {
        $data = $this->request->param();
        $page = input('page/d', 1);
        $length = input('limit/d', 20);
        $cort_type = $data['cort_type'];
        if ($cort_type == 1) {
            $sort = 'create_time desc';
        } else {
            $sort = 'create_time';
        }
        $where[] = ['invite_id', '=', $this->memberId];
        $data_list = Db::name('member')->where($where)
            ->order($sort)
            ->paginate($length, false, [
                'page' => $page
            ])->each(function ($item) {
                $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                $item['money'] = Db::name('reward')->where([
                    ['junior_member_id', '=', $item['id']],
                    ['member_id', '=', $this->memberId],
                    ['status', '=', 1],
                ])->sum('money');
                $item['order_num'] = Db::name('reward')->where([
                    ['junior_member_id', '=', $item['id']],
                    ['member_id', '=', $this->memberId],
                    ['status', '=', 1],
                ])->count();
                $team_num = Db::name('member')
                    ->where('invite_id', '=', $item['id'])
                    ->count();
                $item['team_num'] = $team_num;
                return $item;
            });
        msg(0, '查询成功', $data_list);
    }
    //返利订单列表
    public function shouDetailList()
    {
        $page = input('page/d', 1);
        $length = input('limit/d', 20);
        $where[] = ['member_id', '=', $this->memberId];
        $order_list = Db::name('reward')
            ->where($where)
            ->order('create_time desc')
            ->paginate($length, false, [
                'page' => $page
            ])->each(function ($item) {
                $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                $item['member_info'] = Db::name('member')->field('nickname,avatarUrl')->find($item['junior_member_id']);
                $item['goods_info'] = Db::name('goods')->field('name,first_img')->find($item['goods_id']);
                $price = Db::name('goods_attr_value')->where([
                    'goods_id' => $item['goods_id']
                ])->find();
                $item['goods_info']['price'] = $price['price'];
                return $item;
            });
        msg(0, '查询成功', $order_list);
    }

}
